package com.diycode.controller;

import com.diycode.entity.Users;
import com.diycode.service.UsersService;
import com.diycode.utils.MapControl;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Base64;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author diycode
 */
@Controller
@RequestMapping("/users")
public class UsersController {

    private static final String LIST = "users/list";
    private static final String ADD = "users/add";

    @Resource
    private UsersService usersService;

    //跳转列表页面
    @GetMapping("/list")
    public String list() {
        return LIST;
    }

    //查询
    @PostMapping("/query")
    @ResponseBody
    public Map<String, Object> query(@RequestBody Users users) {
        PageInfo<Users> pageInfo = usersService.query(users);
        if (!CollectionUtils.isEmpty(pageInfo.getList())) {
            pageInfo.getList().forEach(item -> {
                if (item.getAvatar() != null) {
                    item.setAvatarPic("data:image/jpg;base64," + Base64.getEncoder().encodeToString(item.getAvatar()));
                }
            });
        }
        return MapControl.getInstance().success().page(pageInfo.getList(), (int) pageInfo.getTotal()).getMap();
    }

    //根据id查询，跳转修改页面
    @GetMapping(value = {"/detail", "/detail/{id}"})
    public String detail(@PathVariable(value = "id", required = false) Integer id, ModelMap modelMap) {
        if (id != null) {
            //查询出要修改的信息
            Users users = usersService.detail(id);
            //将查询出来的数据存储到request域，实现表单回显
            modelMap.addAttribute("users", users);
        }
        return ADD;
    }

    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public Map<String, Object> saveOrUpdate(Users users, @RequestParam(value = "file", required = false) MultipartFile file) {
        try {
            if (file != null) {
                users.setAvatar(file.getBytes());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Boolean result = usersService.saveOrUpdate(users);
        if (result) {
            return MapControl.getInstance().success().getMap();
        }
        return MapControl.getInstance().error().getMap();
    }

    //根据id删除
    @PostMapping("/delete/{id}")
    @ResponseBody
    public Map<String, Object> delete(@PathVariable("id") Integer id) {
        int result = usersService.delete(id);
        if (result <= 0) {
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    //批量删除
    @PostMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(String ids) {
        int result = usersService.delete(ids);
        if (result <= 0) {
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

}
